<template>
  <div class="app-container">
    <eHeader :query="query" :dic_list="dic_list"/>
    <el-row :gutter="28">
      <el-col :span="span">
        <!--表格渲染-->
        <el-table v-loading="loading" :data="data" size="small" border style="width: 100%;" @current-change="handleCurrentChange">
          <el-table-column label="序号" width="60" align="center">
            <template slot-scope="scope">
              <div>{{ scope.$index + 1 }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="template_name" label="模板名称"/>
          <el-table-column align="center" prop="sign_name" label="模板签名"/>
          <el-table-column align="center" prop="template" label="模板内容"/>
          <el-table-column align="center" prop="template_type_name" label="模板类型">
            <!--<template
              slot-scope="scope"
            >{{ scope.row.template_type == '1' ? '验证码': scope.row.template_type == '2' ? '短信通知':'推广短信' }}
            </template>-->
          </el-table-column>
          <el-table-column align="center" prop="object_type_name" label="发送对象"/>
          <el-table-column align="center" prop="send_type_name" label="发送方式"/>
          <el-table-column align="center" prop="status" label="是否有效">
            <template
              slot-scope="scope"
            >{{ scope.row.status == '1' ? '是':'否' }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" width="160" label="操作" align="center">
            <template slot-scope="scope">
              <edit v-if="checkPermission(['admin','MessageTemplet_all','MessageTemplet_edit'])" :data="scope.row" :sup_this="sup_this" :dic_list="dic_list"/>
              <el-popover
                v-if="checkPermission(['admin','MessageTemplet_all','MessageTemplet_delete'])"
                :ref="scope.row.id"
                placement="top"
                width="180">
                <p>确定删除本条数据吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                  <el-button :loading="Loading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
                </div>
                <el-button slot="reference" type="danger" size="mini">删除</el-button>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination
          :total="total"
          style="margin-top: 8px;"
          layout="total, prev, pager, next, sizes"
          @size-change="sizeChange"
          @current-change="pageChange"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { del } from '@/api/message/messageTemplate'
import { parseTime } from '@/utils/index'
import { getDictList } from '@/api/dictDetail'
import eHeader from './module/header'
import edit from './module/edit'
export default {
  name: 'MessageTemplet',
  components: { eHeader, edit },
  mixins: [initData],
  data() {
    return {
      row_data: null,
      span: 24,
      show: false,
      table_show: true,
      Loading: false,
      sup_this: this,
      dic_list: { send_type: [], template_type: [], object_type: [] }
      // dic_map: { send_type: [], template_type: [], object_type: [] }
    }
  },
  created() {
    this.initDict()
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    initDict() {
      let dictParams = { parent_code: 'TemplateType', is_enabled: 1 }
      // CompleteStatus  CarrierLevel
      getDictList(dictParams).then(res => {
        if (res.code === 200) {
          this.dic_list.template_type = res.data.list
          // this.dic_map.template_type = res.data.value_map
        }
      }).catch(err => {
        console.log(err)
      })
      dictParams = { parent_code: 'TemplateObject', is_enabled: 1 }
      getDictList(dictParams).then(res => {
        if (res.code === 200) {
          this.dic_list.object_type = res.data.list
          // this.dic_map.object_type = res.data.value_map
        }
      }).catch(err => {
        console.log(err)
      })
      dictParams = { parent_code: 'SendType', is_enabled: 1 }
      getDictList(dictParams).then(res => {
        if (res.code === 200) {
          this.dic_list.send_type = res.data.list
          // this.dic_map.send_type = res.data.value_map
        }
      }).catch(err => {
        console.log(err)
      })
    },
    beforeInit() {
      this.url = 'api/message/tamplate/'
      const query = this.query
      const template_name = query.template_name
      const template_type = query.template_type
      this.params = { page: this.page, size: this.size }
      if (template_name) { this.params['template_name'] = template_name }
      if (template_type) { this.params['template_type'] = template_type }
      return true
    },
    handleCurrentChange(val) {
      this.row_data = val
    },
    subDelete(id) {
      this.Loading = true
      del(id).then(res => {
        this.Loading = false
        this.$refs[id].doClose()
        this.init()
        this.$message({
          showClose: true,
          type: 'success',
          message: '删除成功!',
          duration: 2500
        })
      }).catch(err => {
        this.Loading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }
  }
}
</script>

<style>
  .el-card__header {
    font-size: 14px;
  }
</style>
